<template>
  <div>
    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="120px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="单据编号" prop="number">
            <el-input v-model="form.number" :disabled="true" placeholder="自动生成" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="结算数量" prop="settleCount">
            <el-input v-model="form.settleCount" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="结算金额" prop="settleAmount">
            <el-input v-model="form.settleAmount" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="支付日期" prop="payTime">
            <el-date-picker v-model="form.payTime" :disabled="disableForm" type="date" placeholder="选择日期" style="width: 100%;" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="支付方式" prop="payType">
            <el-select v-model="form.payType" :disabled="disableForm" style="width: 100%;" placeholder="请选择支付方式">
              <el-option v-for="item in payTypes" :key="item.name" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="支付单位" prop="payUnit">
            <!-- <el-input v-model="form.payUnit" :disabled="disableForm" /> -->

            <el-select v-model="form.payUnit" :disabled="disableForm" style="width: 100%;" placeholder="请选择支付单位">
              <el-option label="公司" :value="1" />
              <el-option label="分公司" :value="2" />
              <el-option label="项目" :value="3" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" :rows="2" :disabled="disableForm" />
      </el-form-item>

    </el-form>
    <com-table
      v-model="selectRow"
      highlight-current-row
      :data="targetList"
      :columns="columns"
      :no-page="true"
    >
      <template v-slot:number="{row}">
        {{ row.number||row.code }}
      </template>
      <template v-slot:name="{row}">
        <el-input v-model="row.name" />
      </template>
      <template v-slot:specification="{row}">
        <el-input v-model="row.specification" />
      </template>
      <template v-slot:unit="{row}">
        <el-input v-model="row.unit" />
      </template>
      <template v-slot:count="{row}">
        <el-input v-model="row.count" :disabled="disableForm" />
      </template>
      <template v-slot:price="{row}">
        <el-input v-model="row.price" :disabled="disableForm" @input="inputPrice" />
      </template>
      <template v-slot:amount="{row}">
        <el-input v-model="row.amount" :disabled="disableForm" />
      </template>
      <template v-slot:settleCount="{row}">
        <el-input v-model="row.settleCount" :disabled="disableForm" @input="inputSettleCount" />
      </template>
      <template v-slot:settleAmount="{row}">
        <el-input v-model="row.settleAmount" disabled />
      </template>
      <template v-slot:description="{row}">
        <el-input v-model="row.description" :disabled="disableForm" />
      </template>
      <template v-slot:createTime="{row}">
        {{ dateFormat(row.createTime) }}
      </template>
    </com-table>
    <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
    <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />
  </div>
</template>

<script>
import { addMaterialContractSettleInfo, updateMaterialContractSettleInfo, getMaterialContractSettleInfoDetail } from '@/api/contractManage'
import { payTypes, payUnits } from '@/utils/dicData'
import fileUploader from '@/components/FileUploader'
import moment from 'moment'
export default {
  name: 'AddMaterialContractInfo',
  components: {
    fileUploader
  },
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'add'
    },
    dir: {
      type: Object,
      default() {
        return {}
      }
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    },
    id: {
      type: String,
      default() {
        return {}
      }
    },
    relateData: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      showFileModal: false,
      selectRow: null,
      targetList: [],
      payTypes,
      payUnits,

      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        number: '',
        settleCount: '',
        settleAmount: '',
        payTime: '',
        payType: '',
        payUnit: '',
        remark: ''
      },
      columns: [
        {
          label: '材料编号',
          prop: 'number',
          slot: 'number'
        },
        {
          label: '材料名称',
          prop: 'name'
        },
        {
          label: '规格',
          prop: 'specification'
        },
        {
          label: '单位',
          prop: 'unit'
        },
        {
          label: '数量',
          prop: 'count',
          slot: 'count',
          width: '150'
        },
        {
          label: '单价',
          prop: 'price',
          slot: 'price',
          width: '150'
        },
        {
          label: '金额',
          prop: 'amount',
          slot: 'amount',
          width: '150'
        },
        {
          label: '结算数量',
          prop: 'settleCount',
          slot: 'settleCount',
          width: '150'
        },
        {
          label: '结算金额',
          prop: 'settleAmount',
          slot: 'settleAmount',
          width: '150'
        },
        {
          label: '描述',
          prop: 'description',
          slot: 'description',
          width: '150'
        },
        {
          label: '编制时间',
          prop: 'createTime',
          slot: 'createTime',
          width: '90'
        }
      ],
      fileList: [
      ],
      rules: {
        settleCount: [
          { required: true, message: '请输入结算数量', trigger: 'change' }
        ],
        settleAmount: [
          { required: true, message: '请输入结算金额', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    data: {
      handler(newData, oldData) {
        if (newData.businessId) {
          this.getDocDetail(newData.businessId)
        }
      },
      immediate: true
    }
  },
  mounted() {
  },
  methods: {
    dateFormat(data) {
      if (data) {
        return moment(data).format('YYYY-MM-DD')
      } else {
        return ''
      }
    },
    getDocDetail(id) {
      getMaterialContractSettleInfoDetail({ materialContractSettleInfoId: id }).then(res => {
        // const { lstAttachment } = res.data || {}
        if (res.data.lstAttachment && res.data.lstAttachment.length > 0) {
          this.fileList = res.data.lstAttachment
        }
        if (res.data.lstMaterialContractSettleMaterial.length > 0) {
          this.targetList = res.data.lstMaterialContractSettleMaterial
        }
        this.form = res.data.materialContractSettleInfo
      })
    },
    deleteDetail() {
    },
    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const formData = JSON.parse(JSON.stringify(this.form))
          const params = {
            materialContractSettleInfo: {
              ...formData,
              materialContractInfoId: this.relateData.id,
              settleCount: Number(formData.settleCount),
              settleAmount: Number(formData.settleAmount)
              // ...this.optType == 'edit' ? {
              //   id: this.data.id
              // } : {}
            },
            lstMaterialContractSettleMaterial: this.targetList,
            lstAttachment: this.fileList
          }
          if (this.optType === 'edit') {
            params.materialContractSettleInfo.id = this.data.id
          } else {
            delete params.materialContractSettleInfo.id
          }
          const fn = this.optType === 'add' ? addMaterialContractSettleInfo : updateMaterialContractSettleInfo
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    },
    // 数量
    //     inputCount(){
    //   this.targetList.forEach((item,index)=>{
    //     if(item.count && item.price){
    //       item.amount = Number.parseFloat(item.count)  * Number.parseFloat(item.price)
    //     }else{
    //       item.amount = ''
    //     }
    //   })
    //   this.$forceUpdate()
    // },
    inputPrice() {
      this.targetList.forEach((item, index) => {
        if (item.settleCount && item.price) {
          item.settleAmount = Number.parseFloat(item.settleCount) * Number.parseFloat(item.price)
          item.settleAmount = Number.parseFloat(item.settleAmount).toFixed(2)
        } else {
          item.settleAmount = ''
        }
      })
      this.$forceUpdate()
    },
    inputSettleCount() {
      let sum = 0
      let sum2 = 0
      this.targetList.forEach((item, index) => {
        if (item.settleCount && item.price) {
          item.settleAmount = Number.parseFloat(item.settleCount) * Number.parseFloat(item.price)
          item.settleAmount = Number.parseFloat(item.settleAmount).toFixed(2)
        } else {
          item.settleAmount = ''
        }
        if (item.settleCount) {
          sum = sum + Number.parseFloat(item.settleCount)
        } else {
          this.form.settleCount = ''
        }
        if (item.settleAmount) {
          sum2 = sum2 + Number.parseFloat(item.settleAmount)
        } else {
          this.form.settleAmount = ''
        }
      })
      this.form.settleCount = Number.parseFloat(sum).toFixed(2)
      this.form.settleAmount = Number.parseFloat(sum2).toFixed(2)
    }
    //  inputThisPay(){
    //     let sum  = 0
    //   this.targetList.forEach((item,index)=>{
    //     if(item.settleAmount){
    //       sum = sum +Number.parseFloat(item.settleAmount)
    //     }else{
    //       this.form.settleAmount = ''
    //     }
    //   })
    //   this.form.settleAmount = sum
    //  }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
::v-deep .el-form-item__label{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;

}
</style>
